//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.ruoyi.system.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.TUserSys;
import com.ruoyi.system.api.model.LoginUser;
import com.ruoyi.system.domain.vo.TUserSysVo;
import com.ruoyi.system.service.ITUserSysService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(
        tags = {"应用设置"}
)
@RestController
@RequestMapping({"/app"})
public class TUserSysController extends BaseController {
    @Autowired
    private ITUserSysService tUserSysService;

    public TUserSysController() {
    }

    @ApiOperation("应用设置列表")
    @RequiresPermissions({"system:app:list"})
    @GetMapping({"/list"})
    public List<TUserSysVo> list(TUserSysVo tUserSysVo) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tUserSysVo.setUserName(loginUser.getUsername());
        return this.tUserSysService.selectTUserSysList(tUserSysVo);
    }

    @ApiOperation("集成系统列表")
    @RequiresPermissions({"system:app:list"})
    @GetMapping({"/integrations"})
    public List<TUserSysVo> selectIntegrationList() {
        return this.tUserSysService.selectIntegrationList();
    }

    @ApiOperation("应用详情")
    @RequiresPermissions({"system:app:query"})
    @GetMapping({"/info"})
    public AjaxResult getInfo(String cfgId) {
        return AjaxResult.success(this.tUserSysService.selectTUserSysById(cfgId));
    }

    @ApiOperation("应用设置--新增")
    @RequiresPermissions({"system:app:add"})
    @Log(
            title = "应用设置",
            businessType = BusinessType.INSERT
    )
    @PostMapping
    public AjaxResult add(@RequestBody TUserSys tUserSys) {
        return this.toAjax(this.tUserSysService.insertTUserSys(tUserSys.getSysIds()));
    }

    @ApiOperation("应用设置--修改")
    @RequiresPermissions({"system:app:edit"})
    @Log(
            title = "应用设置",
            businessType = BusinessType.UPDATE
    )
    @PostMapping({"/edit"})
    public AjaxResult edit(@RequestBody TUserSys tUserSys) {
        return this.toAjax(this.tUserSysService.updateTUserSys(tUserSys));
    }

    @ApiOperation("应用设置--删除")
    @RequiresPermissions({"system:app:remove"})
    @Log(
            title = "应用设置",
            businessType = BusinessType.DELETE
    )
    @GetMapping({"/remove"})
    public AjaxResult remove(String[] cfgIds) {
        return this.toAjax(this.tUserSysService.deleteTUserSysByIds(cfgIds));
    }

    @RequiresPermissions({"system:app:export"})
    @Log(
            title = "应用设置",
            businessType = BusinessType.EXPORT
    )
    @PostMapping({"/export"})
    public void export(HttpServletResponse response, @RequestBody TUserSysVo tUserSysVo) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tUserSysVo.setUserName(loginUser.getUsername());
        List<TUserSysVo> list = this.tUserSysService.selectTUserSysList(tUserSysVo);
        ExcelUtil<TUserSysVo> util = new ExcelUtil(TUserSysVo.class);
        util.exportExcel(response, list, "应用设置数据");
    }

    @ApiOperation("我的应用")
    @RequiresPermissions({"system:app:list"})
    @GetMapping({"/myApp"})
    public AjaxResult myList() {
        TUserSysVo tUserSysVo = new TUserSysVo();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tUserSysVo.setUserName(loginUser.getUsername());
        return AjaxResult.success(this.tUserSysService.selectTUserSysList(tUserSysVo));
    }

    @RequiresPermissions({"system:app:sort"})
    @GetMapping({"/sort"})
    public AjaxResult sort(String cfgId, Integer rank) {
        this.tUserSysService.updateRankByCfgId(cfgId, rank);
        return this.success();
    }
}
